package top.zhmq.userconsumer.controller;

import org.apache.commons.codec.net.URLCodec;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import top.zhmq.commonmodule.model.User;
import top.zhmq.userconsumer.service.UserService;
import top.zhmq.userconsumer.utils.CodeUtil;
import top.zhmq.userconsumer.utils.MailUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.soap.SAAJResult;
import java.util.concurrent.TimeUnit;

/**
 * 控制层
 *
 * @author Glory
 * @classname UserController
 * @create 2021-06-01 11:21
 */

@Controller
@RequestMapping("/admin")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    MailUtil mailUtil;
    @Autowired
    RedisTemplate redisTemplate;

//    @GetMapping(value = "/resendcode")
//    public String reactive(String uname,String uemail) {
//        User user = new User();
//        user.setUname(uname);
//        user.setUemail(uemail);
//        String code = CodeUtil.randomCode();
//        rabbitTemplate.convertAndSend("fanout_exchange", "", user);
//        redisTemplate.boundValueOps("code_" + user.getUname()).set(code + "");
//        redisTemplate.boundValueOps("code_" + user.getUname()).expire(15, TimeUnit.MINUTES);//设置验证码五分钟到期
//        System.out.println(uname+code);
//        return "active";
//    }

    //跳转登录界面
    @GetMapping(value = "/tologin")
    public String tologin() {
        return "login";
    }

    // 跳转注册界面
    @GetMapping(value = "/toregister")
    public String toregister() {
        return "register";
    }

    // 用户登录
    @GetMapping(value = "/login")
    public String login(String uname, String upassword, HttpServletRequest request) {
        User user = userService.login(uname);
        if (user != null) {
            if (user.getUpassword().equals(upassword) && user.getUvaild() == 1) {
                HttpSession session = request.getSession(true);
                session.setAttribute("user", userService.login(uname));  //将登陆者信息存入session
                return "redirect:http://localhost:8896/goods/getAll?uaccount=" + userService.login(uname).getUaccount() + "&upasswoed=" + userService.login(uname).getUpassword();
            } else if (user.getUvaild() == 0) {
                return "redirect:http://localhost:8893/admin/active";
            }
        }
        request.setAttribute("mag","账号密码错误");
        System.out.print("登录成功");
        return "login";
    }

    @GetMapping(value = "/active")
    public String active(String uname, String ucode, HttpServletRequest request) {
        System.out.println("收到" + ucode);
        if (uname != null && ucode != null) {
            try {
                int res = userService.active(uname, ucode);
                if (res == 0)
                    return "active";
                else
                    return "login";
            } catch (Exception e) {
                return "active";
            }
        }
        return "active";
    }


    //用户注册
    @GetMapping(value = "/register")
    public String register(HttpServletRequest request, String upassword, String uname, String usex, String uemail) {
        if (userService.register(upassword, uname, usex, uemail) > 0) {
            User user = new User();
            user.setUname(uname);
            user.setUpassword(upassword);
            user.setUemail(uemail);
            user.setUsex(usex);
            rabbitTemplate.convertAndSend("fanout_exchange", "", user);
            System.out.print("注册成功,等待激活");
            //通过RabbitMQ告知注册成功，发送激活邮件以及欢迎短信
            System.out.println();
            return "active";
        }
        System.out.print("注册失败");
        return "register";
    }
}
